// Pin.c

#ifndef		PIN_C
#define		PIN_C

#include	"Pin.h"

uint8_t PinOpen(struct sPin *this, uint8_t dir, uint8_t value) {
    PinWrite(this, value);
    PinDir(this, dir);
    return 0;
}

uint8_t PinClose(struct sPin *this) {
    PinDir(this, PIN_INPUT);
    PinWrite(this, PIN_LOW);
    return 0;
}

uint8_t PinRead(struct sPin *this) {
    if ((*this->tris >> this->pin) & 0x01) {
        return (*this->port >> this->pin) & 0x01;
    } else {
        return (*this->lat >> this->pin) & 0x01;
    }
    return 0;

}

void PinWrite(struct sPin *this, uint8_t value) {
	//*this->port = (*this->lat | (0x01 << this->pin)) | (value << this->pin);
	if( value ){
		*this->port = *this->lat | (0x01 << this->pin);
	}else{
		*this->port = *this->lat & ~(0x01 << this->pin);
	}
    
    return;
}

void PinDir(struct sPin *this, uint8_t dir) {
    if (dir) {
        *this->tris |= (0x0001 << this->pin);
    } else {
        *this->tris &= ~(0x0001 << this->pin);
    }
    return;
}

#endif		//#ifndef		PIN_C
